package com.gitee.qdbp.jdbc.stream;

import java.io.Serializable;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.jdbc.api.CrudDao;

/**
 * WhereById查询后续操作<br>
 * 后续操作有: find / logicalDelete / physicalDelete
 *
 * @author zhaohuihua
 * @version 20210529
 */
public class CrudOnAfterByIdStream<T> {

    private final CrudDao<T> dao;
    private final Serializable id;

    CrudOnAfterByIdStream(CrudDao<T> dao, Serializable id) {
        this.dao = dao;
        this.id = id;
    }

    /**
     * 根据主键编号获取对象<br>
     * <pre>
    SysUser user = qdbcBoot.crudStream(SysUser.class)
        .whereById("U0000001")
        .find();
     * </pre>
     * 
     * @return 实体对象
     */
    public T find() {
        return dao.findById(id);
    }

    /**
     * 根据主键编号删除实体对象(逻辑删除)<br>
     * <pre>
    SysUser user = qdbcBoot.crudStream(SysUser.class)
        .whereById("U0000001")
        .logicalDelete();
     * </pre>
     *
     * @return 删除行数
     * @throws ServiceException 删除失败
     */
    public int logicalDelete() throws ServiceException {
        return dao.logicalDeleteById(id);
    }

    /**
     * 根据主键编号删除实体对象(逻辑删除)<br>
     * <pre>
    SysUser user = qdbcBoot.crudStream(SysUser.class)
        .whereById("U0000001")
        .logicalDelete(true, false);
     * </pre>
     *
     * @param fillUpdateParams 是否自动填充更新参数(修改人/修改时间等)
     * @param errorOnUnaffected 受影响行数为0时是否抛异常
     * @return 删除行数
     * @throws ServiceException 删除失败
     */
    public int logicalDelete(boolean fillUpdateParams, boolean errorOnUnaffected) throws ServiceException {
        return dao.logicalDeleteById(id, fillUpdateParams, errorOnUnaffected);
    }

    /**
     * 根据主键编号删除实体对象(物理删除)<br>
     * <pre>
    SysUser user = qdbcBoot.crudStream(SysUser.class)
        .whereById("U0000001")
        .physicalDelete();
     * </pre>
     *
     * @return 删除行数
     * @throws ServiceException 删除失败
     */
    public int physicalDelete() throws ServiceException {
        return dao.physicalDeleteById(id);
    }

    /**
     * 根据主键编号删除实体对象(物理删除)<br>
     * <pre>
    SysUser user = qdbcBoot.crudStream(SysUser.class)
        .whereById("U0000001")
        .physicalDelete(true); // 受影响行数为0时抛异常
     * </pre>
     *
     * @param errorOnUnaffected 受影响行数为0时是否抛异常
     * @return 删除行数
     * @throws ServiceException 删除失败
     */
    public int physicalDelete(boolean errorOnUnaffected) throws ServiceException {
        return dao.physicalDeleteById(id, errorOnUnaffected);
    }
}
